package SweetPea::Roots;

use warnings;
use strict;
use File::Find;

=head1 NAME

SweetPea::Roots - The SweetPea Controller Path & Action Dispatcher!

=head1 VERSION

Version 0.01

=cut

our $VERSION = '0.01';

sub new {
    my ($class, $base) = @_;
    my $self  = bless {}, $class;
       $self->{base} = $base;
    return $self;
}

=head1 SYNOPSIS

This module is used to load controllers and their actions into the action hash for url matching.

Perhaps a little code snippet.

    use SweetPea::Roots;

    my $foo = SweetPea::Roots->new();
    ...

=head1 FUNCTIONS

=head2 load_path_and_actions

=cut

sub load_path_and_actions {
    my $self = shift;
    my $base = $self->{base};
    
    if ( !$base->application->{actions} )
    {
        my $actions = {};
        find(\&_load_path_actions, $base->application->{path} .'/mvc/Controller'); 
        sub _load_path_actions {
            my $name = $File::Find::name;
            my $magic = '';
            my @dir = ();
            if ( $name =~ /.pm$/ )
            {
                require $name;
                my $controller = $name;
                   $controller =~ s/\\/\//g; # convert non-unix paths
                   $controller =~ s/.*Controller\/(.*)\.pm$/$1/;
                my $controller_ref = $controller;
                   $controller_ref =~ s/\//\:\:/g;
                @dir = split /\//, $controller;
                open(INPUT, "<", $name) or die "Couldn't open $name for reading: $!\n";
                my @code = <INPUT>;
                my @routines = grep { /^sub(.*)\{/ } @code;
                   $_ =~ s/sub//g foreach @routines;
                   $_ =~ s/[^a-zA-Z0-9\_\-]//g foreach @routines;
                foreach ( @routines )
                {
                        my $code = '$actions->{lc("/$controller/$_")} = \&' . 'Controller::' . $controller_ref . '::' . $_;
                        eval $code;
                }
                close(INPUT);
            }
        }
        $base->application->{actions} = $actions;
    }
    return $base->application->{actions};
}

=head1 AUTHOR

Al Newkirk, C<< <al.newkirk at awnstudio.com> >>

=head1 BUGS

Please report any bugs or feature requests to C<bug-sweetpea-Roots at rt.cpan.org>, or through
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=SweetPea-Roots>.  I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.




=head1 SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc SweetPea::Roots


You can also look for information at:

=over 4

=item * RT: CPAN's request tracker

L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=SweetPea-Roots>

=item * AnnoCPAN: Annotated CPAN documentation

L<http://annocpan.org/dist/SweetPea-Roots>

=item * CPAN Ratings

L<http://cpanratings.perl.org/d/SweetPea-Roots>

=item * Search CPAN

L<http://search.cpan.org/dist/SweetPea-Roots/>

=back


=head1 ACKNOWLEDGEMENTS


=head1 COPYRIGHT & LICENSE

Copyright 2009 Al Newkirk, all rights reserved.

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.


=cut

1; # End of SweetPea::Roots
